package com.zclcs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域设置
 * 
 * @author 周成龙 addMapping("/**") 对接口配置跨域设置 /**代表所有接口 allowedHeaders("*")
 *         允许所有的请求头 allowedMethods("*") 允许所有的方法 也可以设置为("POST", "GET", "PUT",
 *         "OPTIONS", "DELETE") allowedOrigins("*") 允许所有的域（源地址）
 */
@Configuration
public class WebMvcConfig {
	/*
	 * @Override public void addCorsMappings(CorsRegistry registry) {
	 * registry.addMapping("/**") .allowedHeaders("*") .allowedMethods("*")
	 * .allowedOrigins("http://192.168.1.163:8890") .allowCredentials(true); }
	 */
	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");
		corsConfiguration.setAllowCredentials(true);
		return corsConfiguration;
	}

	/*
	 * 跨域过滤器
	 * 
	 * @return
	 */
	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}
}